var needle = require('needle');
var config = require('../config').config;

var option = {headers: {'Content-Type': 'application/json'}}
/**
 *  得到用户基本信息
 * @param token
 * @param openId
 * @param callback
 */
exports.getUserInfo = function(token, openId, callback){
    var url = 'https://api.weixin.qq.com/cgi-bin/user/info?' +
        'access_token='+token+'&openid='+openId+'&lang=zh_CN';
    needle.get(url, option, function(err, resp, body){
        if(err) return callback(err);
        callback(err, body);
    })
}
/**
 * 推送客服消息
 * @param token
 * @param openId
 * @param content
 * @param callback
 */
exports.sendCustomerTextMsg = function(token, openId, content, callback){
    var url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='+token
      , postData = '{"touser":"'+openId+'","msgtype":"text","text":{"content":"'+content+'"}}';
    needle.post(url, postData, option, function cb(err,resp,body){
        callback(err, body);
    });
}

exports.getAccesstoken = function(callback){
    var url = 'https://api.weixin.qq.com/cgi-bin/token?' +
        'grant_type=client_credential&appid='+config.wechat_appid+'&secret='+config.wechat_secret;
    needle.get(url, option, function(err, resp, body){
        if(err) return callback(err);
        callback(err, body);
    })
}